package com.coocaa.ops.api.common.util;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.coocaa.ops.api.v1.content.model.output.plugin.PluginInfo;
import com.coocaa.ops.api.v1.home.cache.IModuleCache;
import com.coocaa.ops.common.core.biz.module.constant.ModuleTypeConstants;
import com.coocaa.ops.common.core.biz.module.entity.ModuleApkPluginInfo;
import com.coocaa.ops.common.core.biz.module.entity.ModuleInfoEntity;
import com.coocaa.ops.common.core.biz.plugin.entity.PluginInfoEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author tangminghong
 * @date 2020-09-15
 */
@Component
public class PluginUtil {

    private static IModuleCache moduleCache;

    @Autowired
    public PluginUtil(IModuleCache moduleCache) {
        PluginUtil.moduleCache = moduleCache;
    }

    public static PluginInfo handleModulePluginInfo(ModuleInfoEntity moduleInfoEntity) {
        if (moduleInfoEntity != null && moduleInfoEntity.getModuleType() != null) {
            List<ModuleApkPluginInfo> apkPluginList = moduleCache.getModuleApkPluginInfo(moduleInfoEntity.getModuleType());
            if (CollectionUtils.isNotEmpty(apkPluginList) && apkPluginList.size() >= 1) {
                handleModuleType(moduleInfoEntity, apkPluginList.get(0));
                PluginInfo result = convertApkPlugin(apkPluginList.get(0));
                if (result != null) {
                    return result;
                }
            }
        }
        return getDefaultPluginInfo(moduleInfoEntity);

    }

    public static PluginInfo convertPluginInfo(PluginInfoEntity pluginEntity) {
        if (pluginEntity == null) {
            return null;
        }
        PluginInfo pluginInfo = new PluginInfo();

        pluginInfo.setType(pluginEntity.getType());
        pluginInfo.setPackageName(pluginEntity.getPackageName());
        pluginInfo.setCategory(pluginEntity.getCategory());
        pluginInfo.setData(pluginEntity.getData());
        pluginInfo.setHomeVersion(pluginEntity.getHomeVersion());
        pluginInfo.setPluginVersion(pluginEntity.getPluginVersion());

        String params = pluginEntity.getParams();
        if (StringUtils.isNotBlank(params)) {
            try {
                pluginInfo.setParams(JSONObject.parseObject(params));
            } catch (JSONException e) {
                //do nothing
            }
        }

        return pluginInfo;
    }

    private static PluginInfo convertApkPlugin(ModuleApkPluginInfo apkPlugin) {

        if (apkPlugin == null) {
            return null;
        }
        PluginInfo result = new PluginInfo();

        result.setType("Third");
        result.setPackageName(apkPlugin.getPackageName());
        result.setCategory(apkPlugin.getCategory());
        result.setHomeVersion(apkPlugin.getHomeVersion());
        result.setPluginVersion(apkPlugin.getPluginVersion());
        result.setParams(apkPlugin.getParams());
        result.setData(apkPlugin.getData());
        return result;
    }

    private static void handleModuleType(ModuleInfoEntity module, ModuleApkPluginInfo apkPluginInfo) {
        if (module != null && apkPluginInfo != null) {
            module.setModuleType(apkPluginInfo.getModuleTypeClient());
        }
    }

    private static PluginInfo getDefaultPluginInfo(ModuleInfoEntity moduleInfoEntity) {
        PluginInfo pluginInfo = new PluginInfo();
        pluginInfo.setHomeVersion(1);
        pluginInfo.setPluginVersion(1);
        pluginInfo.setType("Third");
        if (moduleInfoEntity != null && moduleInfoEntity.getModuleType() != null) {
            switch (moduleInfoEntity.getModuleType()) {
                case ModuleTypeConstants.MOVIE:
                    pluginInfo.setPackageName("com.tianci.movieplatform"); break;
                case ModuleTypeConstants.EDUCATE:
                    pluginInfo.setPackageName("com.coocaa.educate"); break;
                case ModuleTypeConstants.SHORT_VIDEO:
                    pluginInfo.setPackageName("com.cheese.tv.yst"); break;
                case ModuleTypeConstants.SHOPPING_MALL:
                    pluginInfo.setPackageName("com.coocaa.mall"); break;
                case ModuleTypeConstants.APP_STORE:
                    pluginInfo.setPackageName("com.tianci.appstore"); break;
                case ModuleTypeConstants.AIOT:
                    pluginInfo.setPackageName("com.skyworth.smarthome_tv"); break;
                case ModuleTypeConstants.MUSIC:
                    pluginInfo.setPackageName("com.coocaa.karaoke");
                    moduleInfoEntity.setModuleType(ModuleTypeConstants.DEFAULT);
                    break;
                default:
                    pluginInfo.setType("Self");
            }
        } else {
            pluginInfo.setType("Self");
        }
        return pluginInfo;
    }
}
